package org.dreamwork.assistant.tools.cli.gui;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import org.dreamwork.assistant.tools.cli.Distributor;
import org.dreamwork.assistant.tools.persistence.gui.SwingTool;
import org.dreamwork.util.StringUtil;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class ExecuteDialog extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextPane txtLogger;

    public ExecuteDialog (final String[] args) {
        setContentPane (contentPane);
        setModal (true);
        getRootPane ().setDefaultButton (buttonOK);

        buttonOK.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent e) {
                onOK ();
            }
        });

        buttonCancel.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent e) {
                onCancel ();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation (DO_NOTHING_ON_CLOSE);
        addWindowListener (new WindowAdapter () {
            public void windowClosing (WindowEvent e) {
                onCancel ();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction (new ActionListener () {
            public void actionPerformed (ActionEvent e) {
                onCancel ();
            }
        }, KeyStroke.getKeyStroke (KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        setSize (600, 300);

        final PrintStream ps = System.out;

        final PrintStream p = new PrintStream (new ByteArrayOutputStream () {
            @Override
            public synchronized void write (int b) {

            }

            @Override
            public synchronized void write (byte[] b, int off, int len) {

            }

            @Override
            public synchronized void writeTo (OutputStream out) {

            }
        }) {
            Document doc = txtLogger.getStyledDocument ();

            @Override
            public void println (String x) {
                super.println (x);
                try {
                    if (!StringUtil.isEmpty (x))
                        doc.insertString (doc.getLength (), x, null);
                    doc.insertString (doc.getLength (), "\r\n", null);
                } catch (BadLocationException e) {
                    e.printStackTrace ();
                }
            }

            @Override
            public void println () {
                println ("");
            }
        };


        new Thread (new Runnable () {
            @Override
            public void run () {
                try {
                    System.setOut (p);
                    Distributor.main (args);
                    System.out.println ();
                    System.out.println ();
                    System.out.println ("All job done.");
                    SwingUtilities.invokeLater (new Runnable () {
                        @Override
                        public void run () {
                            buttonOK.setEnabled (true);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace ();
                } finally {
                    System.setOut (ps);
                }
            }
        }).start ();
    }

    private void onOK () {
        // add your code here
        dispose ();
    }

    private void onCancel () {
        // add your code here if necessary
        dispose ();
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$ ();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$ () {
        contentPane = new JPanel ();
        contentPane.setLayout (new GridLayoutManager (2, 1, new Insets (10, 10, 10, 10), -1, -1));
        final JPanel panel1 = new JPanel ();
        panel1.setLayout (new GridLayoutManager (1, 2, new Insets (0, 0, 0, 0), -1, -1));
        contentPane.add (panel1, new GridConstraints (1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, 1, null, null, null, 0, false));
        final Spacer spacer1 = new Spacer ();
        panel1.add (spacer1, new GridConstraints (0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
        final JPanel panel2 = new JPanel ();
        panel2.setLayout (new GridLayoutManager (1, 2, new Insets (0, 0, 0, 0), -1, -1, true, false));
        panel1.add (panel2, new GridConstraints (0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        buttonOK = new JButton ();
        buttonOK.setEnabled (false);
        buttonOK.setText ("OK");
        panel2.add (buttonOK, new GridConstraints (0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        buttonCancel = new JButton ();
        buttonCancel.setText ("Cancel");
        panel2.add (buttonCancel, new GridConstraints (0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        final JPanel panel3 = new JPanel ();
        panel3.setLayout (new GridLayoutManager (1, 1, new Insets (0, 0, 0, 0), -1, -1));
        contentPane.add (panel3, new GridConstraints (0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        final JScrollPane scrollPane1 = new JScrollPane ();
        panel3.add (scrollPane1, new GridConstraints (0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
        txtLogger = new JTextPane ();
        txtLogger.setEditable (false);
        scrollPane1.setViewportView (txtLogger);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$ () {
        return contentPane;
    }
}
